﻿using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UCore;

namespace Twin {
    public partial class frmMisc : Form {
        

        public string GeneratedString { get; private set; }
        private string[] arg;
        //private string args;

        private int INST_BITS = 0;
        private int INST_OP = 1;
        private int INST_CMFG = 2;
        private int INST_PARA = 3;
        private int INST_PARA1 = 4;


        string[] inst = {  "OP_SET_FACTORY",
                        "OP_SET_RESTART",
                        "OP_SET_MONITOR_THIS",
                        "OP_INST_EXEC",
                        "OP_INST_SL651",
                        "OP_INST_REMOTE",
                        "MSG_GLASS"
                            };


        /*private void setFocus() {
            for (int i = 0; i < inst.Length; i++) {
                if (arg[0].Equals(inst[i])) {
                    cmbMiscInst.SelectedIndex = i;
                    break;
                }
            }
        }*/

        private XPort xport = new XPort();


        public frmMisc(XPort xport, string content) {      //OP_RD_BELT, [cmfg], [i_fmt]
            InitializeComponent();
            
            GeneratedString = content;
            this.xport = xport;

            arg = content.Split(',');
            //
            arg[INST_CMFG] = UI.init_cmbCmfg(cmbCmfg, arg[INST_CMFG]);
            //UI.init_cmbCom(cmbCom, Ini.defaultPort);

            this.btnDo.Text = "do " + arg[1];
            //UI.init_cmbMiscInst(cmbMiscInst, inst);
            //setFocus();            
        }


        private void frmMisc_Load(object sender, EventArgs e) {
            TopMost = true;
            FormBorderStyle = FormBorderStyle.FixedSingle; // 设置窗体边框样式为固定单线框
            MaximizeBox = false;   // 移除最大化按钮
            MinimizeBox = false;   // 禁止调整大小

            TConsole.PositionFormAtTopRight(this);
        }


        private void cmbCmfg_SelectedIndexChanged(object sender, EventArgs e) {
            arg[INST_CMFG] = cmbCmfg.SelectedItem.ToString();
            if (arg[INST_CMFG].Equals("空")) {
                arg[INST_CMFG] = "";
            }
        }

        /*
        private void btnSetFactory_Click(object sender, EventArgs e) {
            args = arg[INST_BITS] + "," + arg[INST_OP] + "," + arg[INST_CMFG];      // + "," + arg[INST_IFMT];
            int iPort = int.Parse(cmbCom.Text.Replace("com", "")) - 1;
            TConsole.sendAsProtocol(iPort, args, "cobra");
        }

        private void btnSetRestart_Click(object sender, EventArgs e) {
            args = arg[INST_BITS] + "," + arg[INST_OP] + "," + arg[INST_CMFG];      // + "," + arg[INST_IFMT];
            int iPort = int.Parse(cmbCom.Text.Replace("com", "")) - 1;
            TConsole.sendAsProtocol(iPort, args, "cobra");
        }

        private void btnMonitorThis_Click(object sender, EventArgs e) {
            args = arg[INST_BITS] + "," + arg[INST_OP] + "," + arg[INST_CMFG];      // + "," + arg[INST_IFMT];
            int iPort = int.Parse(cmbCom.Text.Replace("com", "")) - 1;
            TConsole.sendAsProtocol(iPort, args, "cobra");
        }

        private void btnExec_Click(object sender, EventArgs e) {
            args = arg[INST_BITS] + "," + arg[INST_OP] + "," + arg[INST_CMFG];      // + "," + arg[INST_IFMT];
            int iPort = int.Parse(cmbCom.Text.Replace("com", "")) - 1;
            TConsole.sendAsProtocol(iPort, args, "cobra");
        }

        private void btnInst651_Click(object sender, EventArgs e) {
            args = arg[INST_BITS] + "," + arg[INST_OP] + "," + arg[INST_CMFG] + "," + arg[INST_PARA] + "," + arg[INST_PARA1];
            int iPort = int.Parse(cmbCom.Text.Replace("com", "")) - 1;
            TConsole.sendAsProtocol(iPort, args, "cobra");
        }

        private void btnGlass_Click(object sender, EventArgs e) {
            args = arg[INST_BITS] + "," + arg[INST_OP] + "," + arg[INST_CMFG];
            int iPort = int.Parse(cmbCom.Text.Replace("com", "")) - 1;
            TConsole.sendAsProtocol(iPort, args, "cobra");
        }
        */

        
        private void btnDo_Click(object sender, EventArgs e) {
            string opargs;

            switch (arg[INST_OP]) {
                case "OP_SET_FACTORY":
                    opargs = arg[INST_OP] + "," + arg[INST_CMFG];
                    break;

                case "OP_SET_RESTART":
                    opargs = arg[INST_OP] + "," + arg[INST_CMFG];
                    break;

                case "OP_SET_MONITOR_THIS":
                    opargs = arg[INST_OP] + "," + arg[INST_CMFG];
                    break;

                case "OP_INST_EXEC":
                    opargs = arg[INST_OP] + "," + arg[INST_CMFG];
                    break;

                case "OP_INST_SL651":
                    opargs = arg[INST_OP] + "," + arg[INST_CMFG] + "," + arg[INST_PARA] + "," + arg[INST_PARA1];
                    break;

                case "OP_INST_REMOTE":
                    opargs = arg[INST_OP] + "," + arg[INST_CMFG] + "," + arg[INST_PARA];   
                    break;

                case "MSG_GLASS":
                    opargs = arg[INST_OP] + "," + arg[INST_CMFG];
                    break;

                default:
                    MessageBox.Show("Error:unexpected inst");
                    return;
            }
            //int iPort = int.Parse(cmbCom.Text.Replace("com", "")) - 1;
            CInfo cInfo = new CInfo();

            Msg.setInfo_bits(ref cInfo, arg[INST_BITS]);
            cInfo.sOpArgs = opargs;
            string wr_kvs = "";

            TConsole.sendAsProtocol(xport, ref cInfo, wr_kvs, "cobra");
        }
    }
}
